//
//  RefundViewController.m
//  B2C
//
//  Created by 李小龙 on 2017/8/3.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "RefundViewController.h"
#import "OrderDetailTableViewCell.h"

@interface RefundViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) SZTextView *contentTextView;

@end

@implementation RefundViewController

#pragma mark - ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"申请退款";
    [self hiddenShadowImage:NO];
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonAction:)];
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIColor ThemeColor] forKey:NSForegroundColorAttributeName];
    [right setTitleTextAttributes:dic forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = right;
    
    [self createUI];
    
}

#pragma mark - action
- (void)rightBarButtonAction:(id)sender {
    if (!TextIsNotNil(self.contentTextView.text)) {
        [MBProgressHUD myShowHUDAddedTo:self.view labelText:@"请填写退货原因" animated:YES];
        return;
    }
    
    [self.view endEditing:YES];
    [MBProgressHUD showHUDProgress:self.view animated:YES];
    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
    [parameters setObject:self.contentTextView.text forKey:@"rcontent"];
    if (self.detailModel.ordergoods) {
        [parameters setObject:self.detailModel.ordergoods.firstObject.ogid forKey:@"ogid"];
    } else {
        [parameters setObject:self.detailModel.ogid forKey:@"ogid"];
    }
    
    [MYAFNetworking POST:URL_Save_Refund parameters:parameters success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject[@"code"] isEqualToString:@"200"]) {
            [MBProgressHUD myShowHUDAddedTo:self.view labelText:@"申请已提交" animated:YES];
            [self popToRootViewController];
        } else {
            [MBProgressHUD showError:self.view title:responseObject[@"message"] code:responseObject[@"code"] animated:YES];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showError:self.view title:@"" code:@"" animated:YES];
    }];
}
- (void)createUI {
    [self.tableView clearExtendCellLine];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return self.detailModel.ordergoods.count;
        case 1:
            return 2;
        case 2:
            return 1;
        default:
            return 0;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderDetailTableViewCell *cell = [OrderDetailTableViewCell refundCellWith:tableView indexPath:indexPath count:self.detailModel.ordergoods.count];
    [cell updateRefundCellWith:self.detailModel indexPath:indexPath count:self.detailModel.ordergoods.count];
    if (indexPath.section == 1 && indexPath.row == 0) {
        self.contentTextView = cell.twelfthContentTextView;
    }
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0:
            return 112;
        case 1:
            switch (indexPath.row) {
                case 0:
                    return 100;
                case 1:
                    return 44;
                default:
                    return 0;
            }
        case 2:
            return UITableViewAutomaticDimension;
        default:
            return 0;
    }
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 160;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section < 2) {
        return 10;
    }
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
    [view setBackgroundColor:[UIColor grayfaColor]];
    return view;
}
#pragma mark -
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
